#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
修复图片引用脚本 - 将PNG引用改为JPG
"""

import os
import re

def fix_image_references(directory="."):
    """
    递归遍历目录，修复所有JS文件中的图片引用
    """
    # 需要修复的文件扩展名
    extensions = ['.js', '.wxml', '.wxss']
    
    # 需要修改的图片文件名（从优化脚本中获取）
    optimized_images = [
        "icon-refresh", "icon_encyclopedia", "icon_ranking", "仙仙", "史培亚", 
        "小桃", "张瑜", "戈麦斯", "欧若拉", "珮琳", "百川", "糖果", "绵绵", 
        "肯肯", "莉莉安", "莫妮卡", "阿波罗", "阿足", "香草"
    ]
    
    fixed_files = []
    
    for root, dirs, files in os.walk(directory):
        for file in files:
            if any(file.endswith(ext) for ext in extensions):
                file_path = os.path.join(root, file)
                
                try:
                    with open(file_path, 'r', encoding='utf-8') as f:
                        content = f.read()
                    
                    original_content = content
                    
                    # 修复特定图片的引用
                    for img_name in optimized_images:
                        # 匹配 /images/图片名.png 的模式
                        pattern = f'/images/{img_name}\\.png'
                        replacement = f'/images/{img_name}.jpg'
                        
                        content = re.sub(pattern, replacement, content)
                    
                    # 如果内容有变化，写回文件
                    if content != original_content:
                        with open(file_path, 'w', encoding='utf-8') as f:
                            f.write(content)
                        
                        fixed_files.append(file_path)
                        print(f"已修复: {file_path}")
                
                except Exception as e:
                    print(f"处理文件 {file_path} 时出错: {e}")
    
    return fixed_files

def main():
    print("开始修复图片引用...")
    
    fixed_files = fix_image_references()
    
    print(f"\n修复完成！共修复了 {len(fixed_files)} 个文件:")
    for file_path in fixed_files:
        print(f"  - {file_path}")

if __name__ == '__main__':
    main() 